package com.atguigu.springcloud.service;


import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * 节俭实现
 */
@Service
@DefaultProperties(defaultFallback = "fallbackMethod")
public class PaymentService {

    public String   getOk(Integer id){
        return (Thread.currentThread().getName() + "\t" + "id："+id + "fallback method execute！");

    }

    @HystrixCommand
//            (fallbackMethod = "getOk",commandProperties = {
//            @HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds" ,value = "3000")
//    })
     public String   getOk5s(Integer id){
         try {
             TimeUnit.SECONDS.sleep(5);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         return (Thread.currentThread().getName() + "\t for 5s " + "id："+id);
    }

    public String fallbackMethod(){
        return "支付侧摆烂，不要给我面子！";
    }


    @HystrixCommand
            (fallbackMethod = "getCircuitBreakerOpen_FallBack",commandProperties = {
            @HystrixProperty(name ="circuitBreaker.enabled" ,value = "true"),
            @HystrixProperty(name ="circuitBreaker.requestVolumeThreshold" ,value = "10"),
            @HystrixProperty(name ="circuitBreaker.sleepWindowInMilliseconds" ,value = "10000"),
            @HystrixProperty(name ="circuitBreaker.errorThresholdPercentage" ,value = "60"),
    })
     public String   getCircuitBreaker(Integer id){
        if(id<0){

            throw new RuntimeException("我主动扔出一个异常\t");
        }
        String uuid = IdUtil.randomUUID();
         return (Thread.currentThread().getName() + "\t With UUID :  " + uuid);
    }

    public String getCircuitBreakerOpen_FallBack(Integer id){
        return "断路器打开，熔断已启动,请等待微服务重新链上---id"+ id;
    }



}
